package com.rw.admin.controller;

import com.rw.common.pojo.Menu;
import com.rw.admin.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @GetMapping("all")
    public Map<String, Object> getMenus(){
        Map<String, Object> map =new HashMap<>();
        map.put("code",0);
       List<Menu> list = menuService.getAllMenus();
        if (list!=null){
            map.put("data",list);
            map.put("count", list.size());
            map.put("msg","");
        }else {
            map.put("code",199);
            map.put("msg","key不存在");
        }
        return map;
    }

    @GetMapping("allFolder")
    public Map<String, Object> getMenusFolders(){
        Map<String, Object> map =new HashMap<>();

        map.put("code",0);
        List<Menu> list = menuService.getMenusFolder();
        if (list!=null){
            Menu m = new Menu(0,0,"菜单顶层","","");
            list.add(0,m);
            map.put("data",list);
            map.put("count", list.size());
            map.put("msg","");
        }else {
            map.put("code",199);
            map.put("msg","文件夹不存在");
        }
        return map;
    }

    @PostMapping("save")
    public Map<String, Object> save(Menu menu){
        Map<String, Object> map =new HashMap<>();
        System.out.println(menu);
        //boolean flag = true;
        boolean flag= menuService.save(menu);
        if (flag){
            map.put("code",0);
            map.put("msg","保存成功");
        }else {
            map.put("code",199);
            map.put("msg","保存失败");
        }
        return map;
    }

    @RequestMapping("/del/{id}")
    public  Map<String, Object> delMenu(@PathVariable Integer id){
        Map<String, Object> map =new HashMap<>();
        boolean flag= menuService.delete(id);
        if (flag){
            map.put("code",0);
            map.put("msg","保存成功");
        }else {
            map.put("code",199);
            map.put("msg","保存失败");
        }
        return map;
    }

}
